// pages/read/read.js
//index.js
const config = require('../../config.js')
const http = require('../../http.js')
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    conferenceData: [],
    bannerData: ["https://cdn.pluslegal.cn/read_img_banner@2x.png"],
    rule: "",
    showModal: false,
    swiperCurrent: 0,
    roomName: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getConferences();
  },
  getConferences: function () {
    var that = this
    http.request(
      config.nonended,
      'GET', null,
      function (res) {
        if (res.data != null && res.data.length > 0) {
          that.setData({
            conferenceData: res.data
          })
          let array = []
          for (var i = 0; i < res.data.length; i++) {
            array.push(res.data[i].bannerUrl)
          }
          that.setData({
            bannerData: array,
            rule: res.data[0].content
          })
        }
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }
    )
  },


  
  onClick: function (e) {
   
    if (!app.globalData.isLogin) {
     
      wx.navigateTo({
        url: '../login/login',
      })
      return
    }
    let type = parseInt(e.currentTarget.dataset.type);
    console.log(type)
    switch (type) {
  
      case 1:
        this.showHint();
        break
      case 2:
        wx.navigateTo({
          url: "../list/list?id="+this.data.conferenceData[this.data.swiperCurrent].id,
        })
        break
      case 3:
        wx.navigateTo({
          url: "../ranking/ranking?id="+this.data.conferenceData[this.data.swiperCurrent].id,
        })
        break
      case 4:
        wx.navigateTo({
          url: "../history/history",
        })
        break
      case 5:
        wx.navigateTo({
          url: "../mine/mine",
        })
        break
      case 6:
        this.hideModal()
        break
      case 7:
        if (this.data.roomName != null && this.data.roomName != "") {
          this.createMeeting(this.data.conferenceData[this.data.swiperCurrent].id, this.data.roomName)
        } else {
          wx.showToast({
            title: '团队名称不能为空',
            icon: 'none'
          })
        }
        break
        //共读
        case 8:
          wx.navigateTo({
            url: "../read/read",
          }) 
    }

  },

  showHint: function () {
    this.setData({
      showModal: true,
      roomName: '',
    })
  },

  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () {},
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  roomNameInput: function (e) {
    this.setData({
      roomName: e.detail.value
    })
  },


  createMeeting: function (id, name) {
    var that = this
    http.request(
      config.createMeeting,
      'POST', {
        conferenceId: id,
        title: name
      },
      function (res) {
        if(res.data != null && res.data !=""){
          wx.navigateTo({
            url: "../room/room?roomId=" + res.data,
          })
          that.hideModal()
        }else{
          wx.showToast({
            title: "房间创建失败",
            icon: 'none',
          })
        }
       
      },
      function (err) {
        wx.showToast({
          title: err,
          icon: 'none',
        })
      }, true
    )
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})